/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sun.util.calendar.Gregorian;

/**
 *
 * @author Elisa
 */
public class Member {

    private String idAnggota, nama, tempatLahir, alamat, noIdentitas;
    private Date tanggallahir;
    private static String NAMA = "[a-zA-z\\s]*";
    private static String TEMPATLAHIR = "[a-zA-z\\s]*";
    private static String NOIDEN = "[0-9]*";

    public Member() {
    }

    public Member(String nama, String tmplhir, String almt, String noiden) throws Exception {
        this.setNama(nama);
        this.setAlamat(almt);
        this.setNoIdentitas(noiden);
        this.setTempatLahir(tmplhir);

    }

    public String getIdAnggota() {
        return idAnggota;
    }

    public void setIdAnggota(String idAnggota) {
        this.idAnggota = idAnggota;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) throws Exception {
        if (nama.equals("")) {
            throw new Exception("Nama masih kosong");
        }
        if (nama.matches(NAMA)) {
            this.nama = nama;
        } else {
            throw new Exception("Inputan nama hanya terdiri dari huruf");
        }
    }

    public String getTempatLahir() {
        return tempatLahir;
    }

    public void setTempatLahir(String tempatLahir) throws Exception {
        if (tempatLahir.matches(TEMPATLAHIR)) {
            this.tempatLahir = tempatLahir;
        } else {
            throw new Exception("Masukan untuk tempat lahir harus berupa huruf");
        }
    }

    public String getAlamat() {
        return alamat;
    }

    public void setAlamat(String alamat) throws Exception {
        if (alamat.equals("")) {
            throw new Exception("Alamat masih kosong");
        } else {
            this.alamat = alamat;
        } 
    }
    public String getNoIdentitas() {
        return noIdentitas;
    }

    public void setNoIdentitas(String noIdentitas) throws Exception {
        if (noIdentitas.equals("")) {
            throw new Exception("No Identitas masih kosong");
        }
        if (noIdentitas.matches(NOIDEN)) {
            this.noIdentitas = noIdentitas;
        } else {
            throw new Exception("No identitas hanya terdiri dari angka");
        }
    }

    public Date getTanggallahir() {
        return tanggallahir;
    }

    public void setTanggallahir(Date tgl) throws Exception {
        if (tgl != null) {
            if (tgl.before(new GregorianCalendar().getTime())) {
                this.tanggallahir = tgl;
            } else {
                throw new Exception("Tanggal lebih dari hari ini");
            }
        } else {
            throw new Exception("Masukan tanggal lahir anda");
        }

    }

    public void showMessageValid() {
        JOptionPane.showMessageDialog(null, "Data berhasil disimpan");
    }

    public void saveDataMember() throws SQLException {
        PreparedStatement pt = null;
        Connection conn = DatabaseConnection.getConnection();
        java.sql.Date date = new java.sql.Date(this.tanggallahir.getYear(), this.tanggallahir.getMonth(), this.tanggallahir.getDate());
        String sql = "insert into member values(?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1, this.idAnggota);
        pt.setString(2, this.nama);
        pt.setString(3, this.noIdentitas);
        pt.setDate(4, date);
        pt.setString(5, this.alamat);
        pt.executeUpdate();

    }

    public static String IDMember() {
        String IDMember = "";
        try {
            String sql = "select IDMEMBER from MEMBER order by IDMEMBER desc";
            Connection conn = DatabaseConnection.getConnection();

            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                IDMember = (Integer.parseInt(rs.getString(1).substring(3)) + 1) + "";
                while (IDMember.length() < 3) {
                    IDMember = "0" + IDMember;
                }
                IDMember = "mm-" + IDMember;
            } else {
                IDMember = "mm-0001";
            }

        } catch (SQLException ex) {
            Logger.getLogger(Buku.class.getName()).log(Level.SEVERE, null, ex);
        }
        return IDMember;
    }
}
